package cjgl.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cjgl.entity.Teacher;
import cjgl.service.TeacherService;

/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TeacherService teacherService = new TeacherService();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("update".equals(method)) {
			update(request,response);
		}else if("save".equals(method)) {
			save(request,response);
		}else if("delete".equals(method)) {
			delete(request,response);
		}else if("add".equals(method)) {
			add(request,response);
		}
	}

	
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.getRequestDispatcher("jsp/laoShi/edit.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		teacherService.delete(id);
		response.sendRedirect("mian?page=3");
		
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		String name = request.getParameter("name");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		Integer id=null;
		if(idStr!=null&&!"".equals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		Teacher tb= new Teacher(id,name,password,username);
		teacherService.save(tb);
		response.sendRedirect("mian?page=3");
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		Teacher model = teacherService.findById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/laoShi/edit3.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Teacher> list = teacherService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/laoShi/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
